package com.qf.controller;

import com.qf.pojo.Student;
import com.qf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description: 公用常量
 * @Data: 2022/9/14 10:14
 * @Pacakge: com.qf.controller
 * @ClassName: StudentController
 * @Author: fan
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView();
        List<Student> list = studentService.findAll();
        modelAndView.setViewName("student");
        modelAndView.addObject("students",list);
        return modelAndView;
    }
    @RequestMapping("/findById/{id}")
    public String findById(@PathVariable("id")Integer id, Model model){
        Student student = studentService.findById(id);
        model.addAttribute("student",student);
        return "update";
    }
    @RequestMapping("/toAdd")
    public String insertStudent(){
        return "insert";
    }
    @RequestMapping("update")
    public String updateStudent(Student student){
        studentService.updateOrInsert(student);
        return "redirect:/student/findAll";
    }
    @RequestMapping("/insert")
    public String insertStudent(Student student){
        studentService.updateOrInsert(student);
        return "redirect:/student/findAll";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteStudent(@RequestBody Student student){
        studentService.deleteStudent(student.getId());

        return "success";
    }

}
